package com.edu.juc.bean;

import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;

public class MyVar {
    public volatile Boolean isInit = Boolean.FALSE;

    static final AtomicReferenceFieldUpdater<MyVar, Boolean> updater =
            AtomicReferenceFieldUpdater.newUpdater(MyVar.class, Boolean.class, "isInit");

    public void init() {
        if (!updater.compareAndSet(this, Boolean.FALSE, Boolean.TRUE)) {
            System.out.println(Thread.currentThread().getName() + ":初始化失败");
            return;
        }
        System.out.println(Thread.currentThread().getName() +":初始化成功");
    }

    public void setIsInit(){
        if (isInit){
            System.out.println(Thread.currentThread().getName() + ":初始化失败");
            return;
        }
        isInit = Boolean.TRUE;
        System.out.println(Thread.currentThread().getName() +":成功");
    }
}
